package com.dao.impl;

import com.bean.Admin;
import com.bean.Role;
import com.dao.AdminDao;
import com.dao.BaseDao;
import com.utils.SqlUtils;

import java.util.List;
import java.util.Map;

public class AdminDaoImpl extends BaseDao implements AdminDao {
    @Override
    public List<Admin> selectForPageItems(int begin, int pageSize, String content) {
        if (content == null) {
            content = "";
        }
        content = "%" + content + "%";
        String sql = SqlUtils.pageSql(Admin.class, Role.class, new String[]{"RoleName", "Password"});
        return queryForList(Admin.class, sql, content, begin, pageSize);
    }

    @Override
    public Integer selectForPageTotalCount(String content) {
        if (content == null) {
            content = "";
        }
        content = "%" + content + "%";
        String sql = SqlUtils.countSql(Admin.class, Role.class, new String[]{"RoleName"});
        Number count = (Number) queryForSingleValue(sql, content);
        return count.intValue();
    }

    @Override
    public Admin selectBy(Admin admin) {
        Map.Entry<String, Object[]> entry = SqlUtils.selectBy(admin, Role.class, new String[]{"RoleName","Password"});
        return queryForOne(Admin.class, entry.getKey(), entry.getValue());
    }

    @Override
    public int insert(Admin admin) {
        Map.Entry<String, Object[]> entry = SqlUtils.insertSql(admin, new String[]{"RoleName"});
        return update(entry.getKey(), entry.getValue());
    }

    @Override
    public int deleteByPrimaryKey(Integer id) {
        String sql = "delete from `admin` where `id` = ?";
        return update(sql, id);
    }

    @Override
    public int updateByPrimaryKey(Admin admin) {
        Map.Entry<String, Object[]> entry = SqlUtils.updateSql(admin, new String[]{"RoleName"});
        return update(entry.getKey(),entry.getValue());
    }
}
